﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-8-13
Description:CustomerList.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;

public partial class CustomerListSelector : System.Web.UI.Page
{

    string TableName = "CustomerList";//表名
    static string SqlWhere = " 1=1 ";//过滤条件
    string GetCountSql = "select count(id)  from CustomerList where " + SqlWhere;
    string SqlTable = "select *  from CustomerList where " + SqlWhere + "  order by id desc ";
    string ReturnPage = "CustomerListSelector.aspx";

    string SearchFilter = " and (CustomerName like '%{0}%' or customerAddress0 like '%{0}%' or memo like '%{0}%' or customerTel0 like '%{0}%' or customerwork like '%{0}%' or customerlandline0 like '%{0}%' )";

    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化模型

    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        if (!IsPostBack)
        {
            string act = FTchina.basic.FilterSQL(Request["act"]);
             

            if (qm.WorkPage != ReturnPage)
            {
                qm.TableName = TableName;
                qm.GetCountSql = GetCountSql;
                qm.GetTableSql = SqlTable;
                qm.ReturnPage = ReturnPage;
                qm.SqlWhere = SqlWhere;
                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGroup();
            bindData(qm);

        }
    }


    protected void BindGroup()
    {
        string sql = "select * from CustomerGroup order by id asc";
        using (DataTable dt = FTchina.shujuku.GetDataTable(sql))
        {
            
            ddlsearchcg.DataSource = dt;
            ddlsearchcg.DataTextField = "groupname";
            ddlsearchcg.DataValueField = "id";
            ddlsearchcg.DataBind();
            ddlsearchcg.Items.Add(new ListItem("==请选择==", "null"));
            ddlsearchcg.SelectedIndex = ddlsearchcg.Items.Count - 1;
        }

    }
    
 

 

    #region gvList显示

    private void bindData(FTchina.SearchQuery.QueryModel qm)
    {

        int pCurrentPage = (FTchina.basic.SafeRequest(Request["page"]).Length > 0) ? Convert.ToInt32(FTchina.basic.SafeRequest(Request["page"])) : 1;
        int pageSize = 15;//每页的数据
        int AllCount = Convert.ToInt32(FTchina.shujuku.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
        
        litpager.Text = FTchina.Api.Pager.GetPagerWithPageName(qm.GetTableSql, AllCount, pCurrentPage, pageSize, qm.ReturnPage);
        gvList.DataSource = FTchina.shujuku.ShowForPage(qm.TableName, qm.SqlWhere, pCurrentPage, pageSize);
        gvList.DataBind();
        txbsearch.Text = qm.Key1;

        ddlsearchcg.SelectedValue = qm.Key2;

    }
    /// <summary>
    /// 为了实现<thead> <TH>and <tbody>，满足点击表头排序功能
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void gvList_PreRender(object sender, EventArgs e)
    {
        if (gvList.Rows.Count > 0)
        {
            // 使用<TH>替换<TD> 
            gvList.UseAccessibleHeader = true;
            //This will add the <thead> and <tbody> elements 
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹 
            gvList.HeaderRow.TableSection = TableRowSection.TableHeader;
            // FooterRow将被<tfoot>包裹 
            gvList.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    #endregion





 

    protected void btnsearch_Click(object sender, EventArgs e)
    {
        string TableName = "CustomerList";//表名
        string SqlWhere = " 1=1  ";//过滤条件

        string key2 = string.Empty;

        if (txbsearch.Text.Trim().Length > 0)
        {
            qm.Key1 = txbsearch.Text.Trim();
            SqlWhere += string.Format(SearchFilter, qm.Key1, qm.Key2);
        }
        else
        {
            qm.Key1 = "";
        }
        if (ddlsearchcg.SelectedValue == "null")
        {

            qm.Key2 = "null";
        }
        else
        {
            qm.Key2 = ddlsearchcg.SelectedValue;
            SqlWhere += " and  customergroupid in (" + qm.Key2 + ")";
        }
        string GetCountSql = "select count(id)  from CustomerList where " + SqlWhere;
        string SqlTable = "select *  from CustomerList where " + SqlWhere + "  order by id desc ";
        string ReturnPage = "CustomerListSelector.aspx";
     //   FTchina.basic.Log(SqlTable);
        qm.TableName = TableName;
        qm.GetCountSql = GetCountSql;
        qm.GetTableSql = SqlTable;
        qm.ReturnPage = ReturnPage;
        qm.SqlWhere = SqlWhere;
        qm.WorkPage = ReturnPage;
        bindData(qm);
    }
}






